import 'package:flutter/material.dart';
import 'package:flutter_getx_cms/utils/size_extension.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import '../../components/task_card/task_card.dart';
import '../../res/assets_path.dart';
import 'logic.dart';

enum TaskType { send, todo }

class GetWorkbenchPage extends StatelessWidget {
  GetWorkbenchPage({Key? key}) : super(key: key);

  /// 使用Get.put()实例化你的类，使其对当下的所有子路由可用。
  final logic = Get.put(GetWorkbenchLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: _buildAppBar(),
      body: _buildBody(),
    );
  }

  _buildAppBar() {
    return TDNavBar(
      title: '工作台'.tr,
      titleColor: Colors.black,
      backgroundColor: Colors.white,
      padding: const EdgeInsets.only(bottom: 0),
      useBorderStyle: true,
      useDefaultBack: false,
    );
  }

  _buildBody() {
    return Column(
      children: [
        _buildTabBar(),
        Expanded(
          child: TabBarView(
            controller: logic.tabController,
            children: [
              _buildSendTaskList(),
              _buildTodoTaskList(),
            ],
          ),
        )
      ],
    );
  }

  _buildSendTaskList() {
    return Obx(() => SmartRefresher(
          controller: logic.sendRefreshController,
          enablePullDown: true,
          enablePullUp: true,
          onRefresh: () async => logic.onSendRefresh(),
          onLoading: () async => logic.onSendLoadMore(),
          header: WaterDropHeader(),
          footer: ClassicFooter(),
          child: ListView.builder(
            itemCount: logic.sendItems.length,
            itemBuilder: (context, index) {
              return ListTile(title: _buildTask(logic.sendItems[index]));
            },
          ),
        ));
  }

  _buildTodoTaskList() {
    return Obx(() => SmartRefresher(
          controller: logic.todoRefreshController,
          enablePullDown: true,
          enablePullUp: true,
          onRefresh: () async => logic.onTodoRefresh(),
          onLoading: () async => logic.onTodoLoadMore(),
          header: WaterDropHeader(),
          footer: ClassicFooter(),
          child: ListView.builder(
            itemCount: logic.todoItems.length,
            itemBuilder: (context, index) {
              return ListTile(title: _buildTask(logic.todoItems[index]));
            },
          ),
        ));
  }

  // 构建单个任务卡片
  Widget _buildTask(String text) {
    return GestureDetector(
      child: Container(
        margin: const EdgeInsets.symmetric(vertical: 4),
        child: TaskCard(),
      ),
      onTap: () => {
        // 通过 Get.to() 传递参数
        Get.toNamed("/webview", arguments: {'title': 'bing'})
      },
    );
  }


  _buildTabBar() {
    return Container(
      height: 44,
      color: Colors.grey,
      child: TDTabBar(
        decoration: const BoxDecoration(
          borderRadius: BorderRadius.only(
            bottomLeft: Radius.circular(10),
            bottomRight: Radius.circular(10),
          ),
          color: Colors.white,
        ),
        tabs: [
          TDTab(text: '我的请求'.tr, size: TDTabSize.large),
          TDTab(text: '待办事宜'.tr, size: TDTabSize.large),
        ],
        controller: logic.tabController,
        dividerHeight: 0,
        onTap: (int index) {},
      ),
    );
  }
}
